package com.heima.behavior.service.impl;

import com.heima.behavior.pojo.ApUnlikesBehavior;
import com.heima.behavior.service.UnLikesBehaviorService;
import com.heima.model.behavior.dto.UnLikesBehaviorDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.utils.common.threadUtil.ThreadLocalUtil;
import com.heima.utils.common.threadUtil.UserInfo;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

@Service
public class UnLikesBehaviorServiceImpl implements UnLikesBehaviorService {
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private MongoTemplate mongoTemplate;

    @Override
    public ResponseResult unlike(UnLikesBehaviorDto dto) {

        //参数校验
        if(dto==null||dto.getArticleId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //线程中拿到用户id
        UserInfo userInfo = ThreadLocalUtil.get();
        if(userInfo==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }

        String user_unlike_key="USER_UNLIKE_"+dto.getArticleId();
        //判断用户是否点过不喜欢
        Boolean flag = stringRedisTemplate.opsForSet().isMember(user_unlike_key, userInfo.getId().toString());
//        if(flag&&dto.getType()==0){
//            //用户点过一次不喜欢,又传了一次不喜欢参数
//            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
//        }
        //点过不喜欢，取消不喜欢操作
        if(flag&&dto.getType()==1){
            //缓存表和数据库
            //删除redis缓存表
            stringRedisTemplate.opsForSet().remove(user_unlike_key,userInfo.getId().toString());
            //更改数据库类型
            //构造数据库查询条件
            Query query = Query.query(Criteria.where("userId").is(userInfo.getId()).and("articleId").is(dto.getArticleId()));
            Update update = new Update();
            update.set("type",1);
            mongoTemplate.updateFirst(query,update,ApUnlikesBehavior.class);
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }
        //未点过不喜欢,点击不喜欢
        if(!flag&&dto.getType()==0){
            //添加到缓存表
            stringRedisTemplate.opsForSet().add(user_unlike_key,userInfo.getId().toString());

            ApUnlikesBehavior apUnlikesBehavior = new ApUnlikesBehavior();
            apUnlikesBehavior.setCreatedTime(new Date());
            apUnlikesBehavior.setArticleId(dto.getArticleId());
            apUnlikesBehavior.setUserId(userInfo.getId().longValue());
            apUnlikesBehavior.setType((short)0);
            mongoTemplate.insert(apUnlikesBehavior);
        }


        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
